/*
 * This file (RxRanfuzz.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package alkwarel.filters;

import processing.core.PImage;
import alkwarel.objects.AkArtspace;
import alkwarel.objects.AkFilter;
import artx.parameters.AxFloatParameter;
import artx.utils.AxLog;
import artx.utils.AxRandom;

/**
 * @author Xavier Gouchet
 *
 */
public class RxRanfuzz extends AkFilter {

	/**
	 * Standard constructor
	 */
	public RxRanfuzz() {
		super(4002, "ranfuzz", new String[]{"fuzz radius","float"});
	}

	/**
	 * @see alkwarel.objects.AkFilter#Apply(alkwarel.objects.AkArtspace)
	 */
	public boolean Apply(AkArtspace a) {
		AxFloatParameter fuzzRad = null;
		try{
			fuzzRad = (AxFloatParameter) a.GetParameterManager().GetElement("fuzz radius");
		}catch (ClassCastException e){
			AxLog.Debug("RxRanfuzz::Apply =>> can't cast parameter \"fuzz radius\" to float");
		}
		float portee = 10; 
		if (fuzzRad != null){
			portee = fuzzRad.GetValue();
		}
		int decx, decy;
		
		// copy artspace in buffer
		PImage buffer = new PImage(a.width,a.height);
		for (int x=0; x<a.width; x++){
			for (int y=0; y<a.height; y++){  
				buffer.set(x,y, a.get(x,y)); 
			}
		}

		for (int x=0; x<a.width; x++){
			for (int y=0; y<a.height; y++){
				decx = (int) (x + AxRandom.RandF(-portee,portee));
				decy = (int) (y + AxRandom.RandF(-portee,portee));
				decx = Math.max(0, Math.min(a.width-1, decx));
				decy = Math.max(0, Math.min(a.height-1, decy));
				a.set(x, y, buffer.get(decx, decy)); 
			}
		}
		return true;
	}

}
